Constructor | |
---|---|
PhotoImage(file = "file-name") | Constructor |
from tkinter import * win = Tk() can = Canvas(win, width = 300, height = 300) can.pack() img = PhotoImage(file="c:\\temp\\amit.gif") can.create_image(20,20, anchor=NW, image=img) win.mainloop()
from tkinter import * from tkinter import filedialog class MyFrame(Tk): def __init__(self): super().__init__() self.b1=Button(self,text="Open",command=self.open) self.b1.pack(fill=X) self.can=Canvas(self,width=200,height=200,bg="white") self.can.pack() def open(self): ftypes=[("Images",".gif; .png")] fnm=filedialog.askopenfilename(filetypes=ftypes) if fnm!="": self.img=PhotoImage(file=fnm) self.can.create_image(0,0,anchor=NW,image=self.img) frm=MyFrame() frm.mainloop()
from tkinter import * class MyFrame(Tk): def __init__(self): super().__init__() self.iname=StringVar() self.iname.set("r") rb1=Radiobutton(self,text="Ram",variable=self.iname, value="r",command=self.show) rb1.grid(row=0,column=0) rb2=Radiobutton(self,text="Laxman",variable=self.iname, value="l",command=self.show) rb2.grid(row=0,column=1) rb3=Radiobutton(self,text="Seeta",variable=self.iname, value="s",command=self.show) rb3.grid(row=0,column=2) self.can=Canvas(self,width=200,height=300) self.can.grid(row=1,column=0,columnspan=3) self.img=PhotoImage(file="c:\\temp\\r.gif") self.can.create_image(20,20,anchor=NW,image=self.img) def show(self): fname=self.iname.get() self.img=PhotoImage(file="c:\\temp\\"+fname+".gif") self.can.create_image(20,20,anchor=NW,image=self.img) win=MyFrame() win.mainloop()